------------------------------------------------//库声明
LIBRARY IEEE; 
USE IEEE.STD_LOGIC_1164.ALL; 
USE IEEE.STD_LOGIC_UNSIGNED.ALL; 
USE IEEE.STD_LOGIC_ARITH.ALL;

------------------------------------------------//实体定义
entity user_interface is
port
(     clk_in: in std_logic;
        key1: in std_logic;
        key2: in std_logic;
		   sel: out std_logic_vector(3 downto 0);
		  show: out std_logic_vector(7 downto 0);
		 fpshu: buffer integer range 130 to 781250
       );
end entity user_interface;

------------------------------------------------//结构体定义
architecture behave of user_interface is

------------------------------------------------//信号量定义
signal     f: integer range 1 to 5999;
signal clk_saomiao: std_logic;
signal   seg_zhi: integer range 0 to 9;
signal seg_zhi_1: integer range 0 to 9;
signal seg_zhi_2: integer range 0 to 9;
signal seg_zhi_3: integer range 0 to 9;
signal seg_zhi_4: integer range 0 to 9;
signal   wei: integer range 0 to 3:=0;
signal  move: integer range 0 to 3:=0;
signal key_en1: std_logic;
signal key_en2: std_logic;
signal count_1: integer range 0 to 500;
signal count_2: integer range 0 to 250000;

begin

    process(clk_in)------------------------------------------------//分频1，产生数码管扫描时钟
    begin
        if rising_edge(clk_in) then
            if count_1=500 then
                count_1<=0;
                clk_saomiao<='1';			
            else
                count_1<=count_1+1;
                clk_saomiao<='0';
            end if;
        end if;
    end process;
 
    process(clk_in)------------------------------------------------//按键消抖
    begin
        if rising_edge(clk_in) then
            if key1='0' or key2='0' then
                if count_2=250000 then count_2<=count_2;
                else count_2<=count_2+1;
                end if;
                if count_2=249999 then 
                    if key1<='0' then 
                        key_en1<='1';
                    end if;
                    if key2<='0' then
                        key_en2<='1';	
                    end if;				 
                else 
                    key_en1<='0';
                    key_en2<='0';
                end if;
            else 
                count_2<=0;
                key_en1<='0';
                key_en2<='0';
            end if;
        end if;
    end process;
 
    process(key_en1)-------------------------------------------数值增大
    begin
        if rising_edge(key_en1) then						
            if wei=0 then
                if seg_zhi_1/=9 then
                    seg_zhi_1<=seg_zhi_1+1;
                else seg_zhi_1<=0;
                end if;  			 
            end if;

            if wei=1 then
                if seg_zhi_2/=9 then
                    seg_zhi_2<=seg_zhi_2+1;
                else seg_zhi_2<=0;
                end if;  			 
            end if;

            if wei=2 then
                if seg_zhi_3/=9 then
                    seg_zhi_3<=seg_zhi_3+1;
                else seg_zhi_3<=0;
                end if;  			 
            end if;

            if wei=3 then
                if seg_zhi_4/=5 then
                    seg_zhi_4<=seg_zhi_4+1;
                else seg_zhi_4<=0;
                end if;  			 
            end if;	
        end if;	
    end process; 
	
    process(key_en2)-------------------------------------------移位
    begin 
        if rising_edge(key_en2) then
            if wei/=3 then wei<=wei+1;
            else wei<=0;
            end if;
        end if;
    end process;

    process(clk_saomiao)----------------------------------------------按键扫描
    begin
        if rising_edge(clk_saomiao) then
            if move/=3 then move<=move+1;				
                f<=seg_zhi_1*1+seg_zhi_2*10+seg_zhi_3*100+seg_zhi_4*1000;
                fpshu<=781250/f;
            else move<=0;
            end if;
		  
            case move is
                when 0=>sel<="0111";
                when 1=>sel<="1011";
                when 2=>sel<="1101";
                when 3=>sel<="1110";
                when others=>sel<="1111";
            end case;

            case move is
                when 0=>seg_zhi<=seg_zhi_4;
                when 1=>seg_zhi<=seg_zhi_3;
                when 2=>seg_zhi<=seg_zhi_2;
                when 3=>seg_zhi<=seg_zhi_1;
                when others=>seg_zhi<=seg_zhi;
            end case;


            case seg_zhi is			
                when 0=>show <= "00000011";
                when 1=>show <= "10011111";
                when 2=>show <= "00100101";
                when 3=>show <= "00001101";
                when 4=>show <= "10011001";
                when 5=>show <= "01001001";
                when 6=>show <= "01000001";
                when 7=>show <= "00011111";
                when 8=>show <= "00000001";
                when 9=>show <= "00011001";
                when others=>show<="11111111";
           end case;

        end if;
    end process;	 

end architecture behave;